﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

public partial class qy_54tss_Admin_User_UserAddMoneyDetail : System.Web.UI.Page
{
    AccountingDetail dal = new AccountingDetail();

    protected void Page_Load(object sender, EventArgs e)
    {
        ValidatorSet validator = new ValidatorSet("Admin");
        validator.SetValidator();
        if (!Page.IsPostBack)
        {
            int cid = HjNetHelper.GetQueryInt("cid", 0);
            
            WebUtility.BindList("sectionid", "sectionname", SectionProxyData.GetSectionList().Where(a=>a.cityid == cid).ToList(), ddlState);
            WebUtility.BindList("id", "classname", SectionProxyData.GetEdelivergroupList().Where(a=>a.Status == cid).ToList(), ddlDeliverGroup);
            WebUtility.BindList("id", "classname", new Accounts().GetsubList().Where(a=>a.Parentid == 0).ToList(), ddlcount);
            if (Request["id"] != null)
            {
                pageType.Text = "查看明细";
                GetData();
            }
            else
            {
                pageType.Text = "新增明细";
                tbAddDate.Text = DateTime.Now.ToShortDateString();

                EAdminInfo admin = UserHelp.GetAdmin();
                if (cid == 0 && admin.CityID > 0)
                {
                    Response.Redirect("UserAddMoneyDetail.aspx?cid=" + admin.CityID);
                }
                else
                {
                    return;
                }

            }
        }
    }

    protected void GetData()
    {
        int id = HjNetHelper.GetQueryInt("id", 0);
        AccountingDetailInfo model = null;
        model = dal.GetModel(id);
        if (model != null)
        {
            if (model.AddMoney > 0)
            {
                WebUtility.SelectValue(ddlpoint, "1");
            }
            else
            {
                WebUtility.SelectValue(ddlpoint, "-1");
            }


            this.tbpoint.Text = Math.Abs(model.AddMoney).ToString();
            tbAddDate.Text = model.AddDate.ToShortDateString();
            tbInve2.Text = model.Inve2;
            WebUtility.SelectValue(ddlcount, model.UserId);
            WebUtility.SelectValue(ddlDeliverGroup, model.PayType);

            WebUtility.SelectValue(ddlState, model.State);
            hfjj_sub_id.Value = model.PayDate.ToString();
        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        AccountingDetailInfo info = new AccountingDetailInfo();
        decimal point = Convert.ToDecimal(tbpoint.Text);
        info.AddMoney = point * Convert.ToInt32(ddlpoint.SelectedValue);
        info.UserId = Convert.ToInt32(ddlcount.SelectedValue);
        info.State =  Convert.ToInt32(ddlState.SelectedValue);
        info.PayType = Convert.ToInt32(ddlDeliverGroup.SelectedValue);
        info.PayDate = Convert.ToInt32(this.hfjj_sub_id.Value);

        info.PayState = HjNetHelper.GetQueryInt("cid", 0);
        info.AddDate = Convert.ToDateTime(tbAddDate.Text);
        info.Inve2 = WebUtility.InputText(tbInve2.Text);
        info.DataId = HjNetHelper.GetQueryInt("id", 0);
       

        if (info.DataId == 0)
        {
            info.Inve1 = UserHelp.GetAdmin().ID;
            if (dal.Add(info) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('添加成功','text:操作成功','250','150','true','2000','true','text');win_close();");
                btSave.Visible = false;
            }
            else
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "tipsWindown('提示','text:操作失败','250','150','true','2000','true','text');");
            }
        }
        else
        {
            if (dal.Update(info) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('修改成功','text:操作成功','250','150','true','2000','true','text');win_close();");
                btSave.Visible = false;
            }
            else
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "tipsWindown('提示','text:操作失败','250','150','true','2000','true','text');");
            }
        }
    }
}
